package com.weiai.wedding.web.user;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.weiai.wedding.entity.user.User;
import com.weiai.wedding.service.ShiroDbRealm.ShiroUser;
import com.weiai.wedding.service.user.UserService;
import com.weiai.wedding.util.ConUtils;
import com.weiai.wedding.util.DataTablesParamUtility;
import com.weiai.wedding.util.DataTablesReturnObject;
import com.weiai.wedding.util.MD5;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

/**
 * LoginController负责打开登录页面(GET请求)和登录出错页面(POST请求)，

 * 真正登录的POST请求由Filter完成,
 * 
 * @author wangwieheng
 */
@Controller
@RequestMapping(value = "/user/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "")
	public String list(Model model){
		return "user/userList";
	}
	
	@RequestMapping(value = "list")
	@ResponseBody
	public DataTablesReturnObject<User> query(Model model,User user,HttpServletRequest req){
		Sort sort = new Sort(Direction.DESC, "id");
		Pageable pageable = DataTablesParamUtility.buildPage(req,sort);
		String sEcho = req.getParameter("sEcho");
		DataTablesReturnObject<User> dataTablesReturnObject = new DataTablesReturnObject<User>();
		String loginName = user.getLoginName();
		if(loginName==null || "".equals(loginName)){
			Page<User> page = userService.findUserAll(pageable);
			dataTablesReturnObject = DataTablesParamUtility.toDataTablesReturnObject(sEcho, page);
		}else{
			Page<User> page = userService.findUserAllByLoginName(loginName,pageable);
			dataTablesReturnObject = DataTablesParamUtility.toDataTablesReturnObject(sEcho, page);
		}
		return dataTablesReturnObject;
	}
	
	@RequestMapping(value = "create")
	public String createForm(Model model){
		model.addAttribute("actionId", "1");
		return "user/userForm";
	}
	
	@RequestMapping(value = "save")
	public String save(User user, RedirectAttributes redirectAttributes) {
		userService.saveUser(user);
		redirectAttributes.addFlashAttribute("message", "创建用户" + user.getLoginName() + "成功");
		return "redirect:/user/user";
	}
	@RequestMapping(value = "update/{id}")
	public String updateForm(@PathVariable("id") String id,Model model) {
		User user = userService.findUser(id);
		model.addAttribute("actionId", "2");
		model.addAttribute("user", user);
		return "user/userForm";
	}
	@RequestMapping(value = "checkLoginName")
	@ResponseBody
	public String checkLoginName(@RequestParam("oldLoginName") String oldLoginName,
			@RequestParam("loginName") String loginName) {
		if (loginName.equals(oldLoginName)) {
			return "true";
		} else if (userService.findUserByLoginName(loginName) == null) {
			return "true";
		}
		return "false";
	}
	
	@RequestMapping(value = "update")
	public String update(User user, RedirectAttributes redirectAttributes) {
		userService.saveUser(user);
		redirectAttributes.addFlashAttribute("message", "修改用户" + user.getLoginName() + "成功");
		return "redirect:/user/user";
	}
	@RequestMapping(value = "view/{id}")
	public String userView(@PathVariable("id") String id,Model model) {
		User user = userService.findUser(id);
		model.addAttribute("user",user);
		return "user/userView";
	}
	
	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") String id, RedirectAttributes redirectAttributes) {
		userService.deleteUser(id);
		redirectAttributes.addFlashAttribute("message", "删除用户成功");
		return "redirect:/user/user";
	}
}
